package com.test.mcdonald;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;

import com.test.mcdonald.GlobalSettings.TypeMenu;

public class Cashier implements Runnable {
	private float cashDesk;
	Queue<ClientMy> queueClients = new LinkedList<ClientMy>();
	boolean isBusy = true;
	private ClientMy currentClientMy;
	private String nameCashier;
	private int timeService;
	private Thread curThread;

	public Thread getCurThread() {
		return curThread;
	}

	public void addCash(final float cashValue) {

	}

	public void serveClient() {
		System.out.print("Start Name cashier:" + this.nameCashier + "\n");
		try {
			synchronized (queueClients) {
				if (queueClients.size() > 0) {
					currentClientMy = queueClients.poll();
					for (TypeMenu typeMenu : Arrays
							.asList(currentClientMy.dish.dishTypes)) {
						// getTypeMenyFromTable();
					}
				}
			}

			Thread.sleep((int)(Math.random()*300));
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.print("Class: " + this.getClass().getSimpleName()
				+ " Method: serveClient" + " Name cashier:" + this.nameCashier
				+ " queue is " + queueClients.size()  + " clients\n");
		isBusy = false;
	}

	public String getNameCashier() {
		return nameCashier;
	}

	public synchronized void setBusy() {
		isBusy = true;
	}

	public synchronized void setClientMy(ClientMy clientMy) {
		currentClientMy = clientMy;
		isBusy = true;

		System.out.print("Class: " + this.getClass().getSimpleName()
				+ " Method: setClientMy" + " Name cashier:" + this.nameCashier
				+ "\n");
	}

	public Cashier(String nameCashier) {
		this.nameCashier = nameCashier;
		this.curThread = new Thread(this, nameCashier);
		this.curThread.start();
	}

	@Override
	public void run() {
		synchronized (this) {
			while (!McDonald.stopWork) {
				if (isBusy) {
					serveClient();
				} else {
					try {
						System.out.print("cashier " + this.nameCashier
								+ " is waiting for client\n");
						wait();
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}

	}

	public void setTimeBusy(int time) {
		this.timeService = time;
	}
}
